package net.digitalartwork.geoslideshow {
	import flash.display.*;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.*;
	import flash.events.*;
	import flash.net.*;
	import flash.text.TextField;
	
	import net.digitalartwork.util.String2;
	import net.digitalartwork.ui.BasicButton;
	import net.digitalartwork.ui.Tooltip;
	//import net.digitalartwork.ui.ImageMaker;
	import net.digitalartwork.ui.ImageMakerLite;
	import net.digitalartwork.ui.SunDraw;
	
	import gs.TweenFilterLite;
	import gs.OverwriteManager;
	import gs.easing.*;
	
	import br.com.stimuli.loading.BulkLoader;
    import br.com.stimuli.loading.BulkProgressEvent;
	
	
	public class DAImageBox extends MovieClip{
		
		//--------------------------------------------------------------------------
		// ASSETS
		//--------------------------------------------------------------------------
		public var image_bounds:MovieClip;
		public var image_holder:MovieClip;
		public var title_text:TextField;
		//--------------------------------------------------------------------------
		// ARRAYS
		//--------------------------------------------------------------------------
		private var image_array:Array = [];
		private var image_array_sort:Array = [];
		//--------------------------------------------------------------------------
		// Properties
		//--------------------------------------------------------------------------
		public var _scale:Number;
		public var _height:Number;
		public var _width:Number;
		public var _spacing:Number = 50;
		public var _imageholder_width:Number = 0;
		
		private var _color:Number = 0x336699;
		private var _colorselect:Number = 0x89B3CF;
		
		private var tXML:XMLList; // list of itemXML
		
		public var current_number:Number = 0;
		
		public var current_image;
		
		public var _ready:Boolean = false;
		//--------------------------------------------------------------------------
		// Dispatch
		//--------------------------------------------------------------------------
		public var IMAGE_CHANGE:String = "imagechange";
		public var MOVE_DONE:String = "movedone";
		
		//--------------------------------------------------------------------------
		// BulkLoader
		//--------------------------------------------------------------------------
		public var iLoader:BulkLoader;
		
		
		public function DAImageBox() {
			trace("DAImageBox Init");

			_scale = this.scaleX;
			this.scaleX = 1;
			this.scaleY = 1;

			
			_height = image_bounds.height;
			_width = image_bounds.width;
			
			image_holder = new MovieClip();
			addChild(image_holder);
			
			image_bounds.alpha = 0;
			
		}
		
		public function populateBox(thumbXML:XML):void {
			tXML = thumbXML.item;
			var i:int = 0;
			
			iLoader = new BulkLoader("images");
			//iLoader.logLevel = BulkLoader.LOG_INFO;
			//trace(thumbXML.item[0].thumb[0])
			
			for each (var item in tXML) {
				var s:String = item.thumb[0];
				iLoader.add(s);
			}
			
			var fst:String = thumbXML.item[0].thumb[0];
			iLoader.get(fst).addEventListener(Event.COMPLETE, onBulkFirstLoad)
			
			
			iLoader.addEventListener(BulkLoader.COMPLETE, onBulkLoadComplete);
			iLoader.addEventListener(BulkLoader.PROGRESS, onBulkLoadProgress);
            
            // now start the loading
            iLoader.start();
		}
		
		private function onBulkFirstLoad(e:Event):void {
			trace("FIRST LOAD");
		}
		
		private function onBulkLoadProgress(e:BulkProgressEvent):void {
			//trace(e.loadingStatus());
		}
		
		private function onBulkLoadComplete(e:Event):void {
			var t:int;
			for each (var item in tXML) {
				makeItem(item, t);
				t++;
				trace("LOADCOMPLETE")
			}
			_ready = true;
			scrollTo(0);
		}
		/*
		private function fixDate(t:String):Date {
			
			var str:String = t;
			//trace(str.replace(myPattern, "sch"));  
			
			str = str.replace("/08 ", "/2008 ");
			str = str.replace("/09 ", "/2009 ");
			str = str.replace("/10 ", "/2010 ");
			str = str.replace("/11 ", "/2011 ");
			str = str.replace("/12 ", "/2012 ");
			str = str.replace("/13 ", "/2013 ");
			str = str.replace("/14 ", "/2014 ");
			
			var d:Date = new Date(str);
			
			return d;
		}
		*/
		
		private function makeItem(ix:XML, t:Number):void {
			trace("MAKE")
			
			var s:String = ix.thumb[0];
			var fi:String = ix.media[0];
			
			var ix_width:Number = ix.meta[0].attribute("width");
			var ix_height:Number = ix.meta[0].attribute("height");
			
			
			var sort_date:Date = String2.fixDate(ix.exif[0].date[0]);
			var sort_object:Object = new Object();
			sort_object["_rating"] = ix.meta[0].attribute("rating");
			sort_object["_color"] = ix.meta[0].attribute("color");
			sort_object["_date"] = sort_date;
			sort_object["_edit"] = t;
			
			var info_object:Object = new Object();
			info_object["_width"] = ix.meta[0].attribute("width");
			info_object["_height"] = ix.meta[0].attribute("height");
			info_object["_media"] = ix.media[0];
			info_object["_thumb"] = ix.thumb[0];
			info_object["_number"] = t;
			
			var tmc:ImageMakerLite = new ImageMakerLite(iLoader.getBitmap(s), sort_object, ix_width, ix_height, t, false, fi, true);
			//var tmc:ImageMaker = new ImageMaker(ix.thumb[0], ix.meta[0].attribute("width"), ix.meta[0].attribute("height"));
			//BulkLoader.getLoader("images").
			//BulkLoader.getLoader("main-site").getContent("bg") as Bitmap;
			image_holder.addChild(tmc);
			image_array.push(tmc);
			image_array_sort.push(tmc);
			
			tmc.setImageXML(ix);

			//tmc.makeBW(.5);
			tmc.makeAlpha(.5);
			tmc.buttonMode = true; 
			tmc.useHandCursor = true;
			//tmc.addEventListener(tmc.IMAGE_LOADED, onImageLoadedHandeler);
			tmc.addEventListener(tmc.IMAGE_ROLLOVER, onThumbRollOver);
			tmc.addEventListener(tmc.IMAGE_ROLLOUT, onThumbRollOut);
			tmc.addEventListener(tmc.IMAGE_DOWN, onThumbDown);
			
			//tmc.x = ((_imageholder_width + _spacing));
			//_imageholder_width = _imageholder_width + tmc.width + _spacing;
			
			sortBox("edit");
		}
		
		private function onImageLoadedHandeler(e:Event):void {
			if (e.target.getNumVar() == 0) {
				_ready = true;
				scrollTo(0);
			}
		}
		public function scaleHeight(h:Number):void {
			
		}
		private function onThumbRollOver(e:Event):void {
			//TweenFilterLite.to(e.target, .2, {alpha:1});
			//e.target.makeAlpha(1)
		};
		private function onThumbRollOut(e:Event):void {
			//e.target.makeAlpha(.5);
		};
		private function onThumbDown(e:Event):void {
			
			trace("onThumbDown")
			current_number = e.target.getNumVar();
			scrollTo(current_number);
			dispatchEvent(new Event(IMAGE_CHANGE));
			
		};
		public function scrollTo(n:Number, noLoad:Boolean=false, tt:Number = 1):void {
			
			
			
			if (_ready) {
				current_number = n;
				var tmc = image_array[n];
				current_image = tmc;
				var tmc_h:int = tmc.getXML().meta[0].attribute("height");
				var tmc_w:int = tmc.getXML().meta[0].attribute("width");


				if (image_bounds.height < tmc_h) {
					trace("SCALE------- "+ image_bounds.height /tmc_h )
					_scale = image_bounds.height /tmc_h;
				} else {
					_scale = 1;
				}
				
				if (image_bounds.width < (tmc_w*_scale)) {
					_scale = image_bounds.width / tmc_w;
				}

				var tmpY:Number = ((image_bounds.height/2) - ((tmc_h*_scale)/2));
				var tmpX:Number = (-tmc.x*_scale) + image_bounds.x + (image_bounds.width/2) - ((tmc.width*_scale)/2)// - (_spacing*_scale));

				TweenFilterLite.to(image_holder, tt, {x:tmpX, y:tmpY, scaleX:_scale, scaleY:_scale, ease:Expo.easeInOut, onComplete:moveDone});
				tmc.makeColor();
				
				for (var i:int = 0; i < image_array.length; i++){
					if (i == current_number) {
						TweenFilterLite.to(image_array[i], .5, {alpha:1, ease:Linear.easeNone});
					} else {
						if (image_array[i].alpha > .35) {
							TweenFilterLite.to(image_array[i], .5, {alpha:.3, ease:Linear.easeNone});
						}
					}

				}

				function moveDone():void {
					dispatchEvent(new Event(MOVE_DONE));
				}
			}
			
			
			
		}
		public function getItemXml(n):XML {
			return image_array[n].getXML()
		}
		
		public function updateDisplay():void {

			
		}
		
		public function sortBox(mthd:String="date"):void {
			
			if (mthd == "date") {
				image_array_sort.sortOn("_date", Array.NUMERIC);
			}
			if (mthd == "edit") {
				image_array_sort.sortOn("_edit", Array.NUMERIC);
			}
			if (mthd == "color") {
				image_array_sort.sortOn("_color", Array.NUMERIC);
			}
			if (mthd == "rating") {
				image_array_sort.sortOn("_rating", Array.NUMERIC);
			}
			
			_imageholder_width = 0;
			
			for (var i:int = 0; i < image_array_sort.length; i++) {
				
				if (mthd == "edit") {
					if (image_array_sort[i]._rating == "nil") {
						//image_array_sort[i].visible = false;
						TweenFilterLite.to(image_array_sort[i], 1, {scaleY:0, ease:Expo.easeInOut, onComplete:hideImage, onCompleteParams:[image_array_sort[i]]});
					} else {
						image_array_sort[i].visible = true;
						TweenFilterLite.to(image_array_sort[i], 1, {scaleY:1, x:((_imageholder_width + _spacing)), ease:Expo.easeInOut});
						_imageholder_width = _imageholder_width + image_array_sort[i].width + _spacing;
					}
				} else {
					image_array_sort[i].visible = true;
					TweenFilterLite.to(image_array_sort[i], 1, {scaleY:1, x:((_imageholder_width + _spacing)), ease:Expo.easeInOut});
					_imageholder_width = _imageholder_width + image_array_sort[i].width + _spacing;
				}
				
			}
			
			function hideImage(remi):void {
				remi.visible = false;
			}
			
			TweenFilterLite.to(image_holder, 1, {alpha:1, ease:Expo.easeInOut, onComplete:scrollTo, onCompleteParams:[current_number]});
			
		}
		
		
		/*
		var vegetables:Array = new Array();
		vegetables.push(new Vegetable("lettuce", 1.49));
		vegetables.push(new Vegetable("spinach", 1.89));
		vegetables.push(new Vegetable("asparagus", 3.99));
		vegetables.push(new Vegetable("celery", 1.29));
		vegetables.push(new Vegetable("squash", 1.44));

		trace(vegetables);
		// lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44

		vegetables.sortOn("name");
		trace(vegetables);
		// asparagus:3.99, celery:1.29, lettuce:1.49, spinach:1.89, squash:1.44

		vegetables.sortOn("price", Array.NUMERIC | Array.DESCENDING);
		trace(vegetables);
		// asparagus:3.99, spinach:1.89, lettuce:1.49, squash:1.44, celery:1.29

		class Vegetable {
		    public var name:String;
		    public var price:Number;

		    public function Vegetable(name:String, price:Number) {
		        this.name = name;
		        this.price = price;
		    }

		    public function toString():String {
		        return " " + name + ":" + price;
		    }
		}
		*/
		
	}
	
}